/*
 * @lc app=leetcode.cn id=1512 lang=typescript
 *
 * [1512] 好数对的数目
 */

// @lc code=start
function numIdenticalPairs(nums: number[]): number {
    let counter = new Map<number, number>();
    for (let i = 0; i < nums.length; i++) {
        const element = nums[i];
        counter.set(element, (counter.get(element) || 0) + 1);
    }

    // 1+2+3 一直加到 num-1，因为是从后面向前看的
    const stair = (num: number) => {
        let counter = 0;
        while (num) {
            num--;
            counter += num;
        }
        return counter;
    };
    let final = 0;
    for (const iterator of counter.values()) {
        final += stair(iterator);
    }
    return final;
}
// @lc code=end
